﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.ReflectionModel;
using System.Globalization;
using System.Reflection;
using Microsoft.Internal;

namespace System.ComponentModel.Composition.AttributedModel
{
    internal class AttributedMemberExportDefinition : ExportDefinition
    {
        private readonly string _contractName;
        private readonly IDictionary<string, object> _metadata;
        private readonly MemberInfo _member;

        public AttributedMemberExportDefinition(string contractName, MemberInfo member)
        {
            Assumes.NotNull(contractName, member);

            this._contractName = contractName;
            this._member = member;            

            // TODO: For now ignore any issues coming from this call they should 
            // end up in the list of discovery issues, when we do the discovery work
            IDictionary<string, object> metadata;
            member.TryExportMetadataForMember(out metadata);
            this._metadata = metadata.AsReadOnly();
        }

        public override string ContractName
        {
            get { return this._contractName; }
        }

        public MemberInfo Member
        {
            get { return _member; }
        }

        public override IDictionary<string, object> Metadata
        {
            get { return this._metadata; }
        }

        public override string ToString()
        {
            return string.Format(
                CultureInfo.InvariantCulture,
                "ContractName={0},DeclaringType={1},MemberName={2}",
                this.ContractName,
                this._member.DeclaringType.FullName,
                this._member.Name);
        }

        public ExportingMember ToExportingMember()
        {
            return new ExportingMember(this, _member.ToReflectionMember());
        }
    }
}
